//
//  WFNetWorkTools.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/10.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit
import AFNetworking

//定义枚举
enum RequestType:Int {
    case GET
    case POST
}

class WFNetWorkTools: AFHTTPSessionManager {
    //定义单例的获取方法
    static let sharedTools:WFNetWorkTools = {
       let tools = WFNetWorkTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //封装图片上传的方法
    func request(url:String,params:AnyObject?,imageData:NSData,name:String,callBack:((response:AnyObject?,error:NSError?)->())){
        //上传图片接口
        POST(url, parameters: params, constructingBodyWithBlock: { (formData) -> Void in
            /**
            imageData : 上传图片所需要二进制数据
            name: 服务端的获取数据的参数
            fileName: 告诉服务端我们的文件名字,服务端一般不会用,他们会自动生成唯一的一个图片名
            mimeType: 资源类型, octet-stream通用的资源类型
            */
            
            formData.appendPartWithFileData(imageData, name: name, fileName: "test", mimeType: "octet-stream")
            
            }, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
            }) { (_, error) -> Void in
                callBack(response: nil, error: error)
        }
    }
    
    
    //封装get和post请求
    func request(type:RequestType,url:String,params:AnyObject?,callBack:((response:AnyObject?,error:NSError?)->())){
        if type == .GET {
            //get请求
            GET(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
               
                }, failure: { (_, error) -> Void in
                 callBack(response: nil, error: error)
            })
        }else{
            //post请求
            POST(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                 callBack(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                callBack(response: nil, error: error)
            })
        }
    
    }
    
    
}

// MARK:  - 首页微博相关接口
extension WFNetWorkTools{
    func loadStatus(maxId:Int64 = 0 ,sinceId:Int64 = 0 ,callBack:((response:AnyObject?,error:NSError?)->())){
        //  准备接口地址
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        //  准备参数
        let params = [
            "access_token": WFUserAccountViewModel.sharedUserAccount.accessToken!,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
            
        ]
        
        print(url + "?access_token=" + WFUserAccountViewModel.sharedUserAccount.accessToken!)
        request(.GET, url: url, params: params, callBack: callBack)
    }


}

// MARK:  - 登录授权相关接口
extension WFNetWorkTools{
    // MARK:  - 获取授权码的请求
    func requsetAccessToken(code:String,callBack:((response:AnyObject?,error:NSError?)->())){
                let urlStr = "https://api.weibo.com/oauth2/access_token"
                let dic = [
                    "client_id":WeiBoAppKey,
                    "client_secret":WeiBoAppSecret,
                    "grant_type":"authorization_code",
                    "code":code,
                    "redirect_uri":WeiBoRedirect_Uri
                ]
        
            request(.POST, url: urlStr, params: dic, callBack: callBack)
       
    }
    
   // MARK:  -  获取用户信息
    func requestUserInfo(userAccount:WFUserAccount,callBack:((response:AnyObject?,error:NSError?)->())){
        //  请求接口地址
        let url = "https://api.weibo.com/2/users/show.json"
        //  准备请求参数
        //  int64,跟字符串放在一起编译不通过,需要转换成字符串
        let params = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
        request(.GET, url: url, params: params, callBack: callBack)

    }
}

// MARK:  - 发送微博相关接口
extension WFNetWorkTools {
    
    func upload(access_token: String, status: String, image:UIImage , callBack: (response: AnyObject?, error: NSError?) -> ()){
        //  准备url地址
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        //  准备参数
        let params = [
            "access_token": access_token,
            "status": status
        ]
        
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        
        //用封装的上传图片的方法进行请求
        request(url, params: params, imageData: imageData, name: "pic", callBack: callBack)
    }
    
    
    /**
     发送文字相关接口
     
     - parameter access_token: 访问令牌
     - parameter status:       文字内容

     - parameter callBack:     回调
     */
    
    func upload(access_token: String, status: String,  callBack: (response: AnyObject?, error: NSError?) -> ()){
        //  准备url地址
        let url = "https://api.weibo.com/2/statuses/update.json"
        //  准备参数
        let params = [
            "access_token": access_token,
            "status": status
            
        ]
        //公共接口进行请求数据
        request(.POST, url: url, params: params, callBack: callBack)

    }

}
